<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从结果集中取得一行作为对象</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.mysql-fetch-object" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_fetch_object</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_fetch_object</span> &mdash; <span class="dc-title">从结果集中取得一行作为对象</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.mysql-fetch-object-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>mysql_fetch_object</strong></span>
       ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>
      ) : <span class="type">object</span></div>

    <p class="para rdfs-comment">
     返回根据所取得的行生成的对象，如果没有更多行则返回 <strong><code>FALSE</code></strong>。
    </p>
    <p class="para">
     <span class="function"><strong>mysql_fetch_object()</strong></span> 和
     <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span>
     类似，只有一点区别 - 返回一个对象而不是数组。间接地也意味着只能通过字段名来访问数组，而不是偏移量（数字是合法的属性名）。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数返回的字段名<em class="emphasis">大小写敏感</em>。</span></p></blockquote>
    <p class="para">
     <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;this&nbsp;is&nbsp;valid&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">field</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;this&nbsp;is&nbsp;invalid&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
     </div>

    </p>
    <p class="para">
     速度上，本函数和
     <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span> 一样，也几乎和
     <span class="function"><a href="mysql_fetch_row.html" class="function">mysql_fetch_row()</a></span> 一样快（差别很不明显）。
     <div class="example" id="example-2141">
      <p><strong>Example #1 <span class="function"><strong>mysql_fetch_object()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"hostname"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;*&nbsp;from&nbsp;mytable"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">user_id</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fullname</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     参见 <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span>，<span class="function"><a href="mysql_fetch_assoc.html" class="function">mysql_fetch_assoc()</a></span>
     和 <span class="function"><a href="mysql_fetch_row.html" class="function">mysql_fetch_row()</a></span>。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.mysql-fetch-object-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<code class="parameter">
result</code></dt>
<dd>
<p class="para"><span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>
型的结果集。此结果集来自对 <span class="function"><a href="mysql_query.html" class="function">mysql_query()</a></span>
的调用。</p></dd>

    
     <dt>
<code class="parameter">class_name</code></dt>

      <dd>

       <p class="para">
        The name of the class to instantiate, set the properties of and return.
        If not specified, a <strong class="classname">stdClass</strong> object is returned.
       </p>
     </dd>

    
    
     <dt>
<code class="parameter">params</code></dt>

      <dd>

       <p class="para">
        An optional <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of parameters to pass to the constructor
        for <code class="parameter">class_name</code> objects.
       </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mysql-fetch-object-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an <span class="type"><a href="language.types.object.html" class="type object">object</a></span> with string properties that correspond to the
   fetched row, or <strong><code>FALSE</code></strong> if there are no more rows.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mysql-fetch-object-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        Added the ability to return as a different object.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mysql-fetch-object-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2142">
    <p><strong>Example #2 <span class="function"><strong>mysql_fetch_object()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"hostname"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;*&nbsp;from&nbsp;mytable"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">user_id</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fullname</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">mysql_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2143">
    <p><strong>Example #3 <span class="function"><strong>mysql_fetch_object()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"hostname"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #DD0000">"mydb"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select&nbsp;name&nbsp;from&nbsp;mytable&nbsp;limit&nbsp;1"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_fetch_object</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mysql-fetch-object-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Performance</strong><br />
   <p class="para">
    Speed-wise, the function is identical to
    <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span>, and almost as quick as
    <span class="function"><a href="mysql_fetch_row.html" class="function">mysql_fetch_row()</a></span> (the difference is
    insignificant).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>mysql_fetch_object()</strong></span> is similar to
    <span class="function"><a href="mysql_fetch_array.html" class="function">mysql_fetch_array()</a></span>, with one difference - an
    object is returned, instead of an array.  Indirectly, that means
    that you can only access the data by the field names, and not by
    their offsets (numbers are illegal property names).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数返回的字段名<em class="emphasis">大小写敏感</em>。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数将 NULL 字段设置为 PHP <strong><code>NULL</code></strong> 值。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mysql-fetch-object-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysql_fetch_array.html" class="function" rel="rdfs-seeAlso">mysql_fetch_array()</a> - 从结果集中取得一行作为关联数组，或数字数组，或二者兼有</span></li>
    <li class="member"><span class="function"><a href="mysql_fetch_assoc.html" class="function" rel="rdfs-seeAlso">mysql_fetch_assoc()</a> - 从结果集中取得一行作为关联数组</span></li>
    <li class="member"><span class="function"><a href="mysql_fetch_row.html" class="function" rel="rdfs-seeAlso">mysql_fetch_row()</a> - 从结果集中取得一行作为枚举数组</span></li>
    <li class="member"><span class="function"><a href="mysql_data_seek.html" class="function" rel="rdfs-seeAlso">mysql_data_seek()</a> - 移动内部结果的指针</span></li>
    <li class="member"><span class="function"><a href="mysql_query.html" class="function" rel="rdfs-seeAlso">mysql_query()</a> - 发送一条 MySQL 查询</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>